<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>添加文章</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/backend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/backend/css/public.css" media="all">
</head>
<body>
<form class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required|title" lay-reqtext="标题不能为空" placeholder="请输入标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">文章分类</label>
        <div class="layui-input-block">
            <select name="category" lay-verify="required">
                <option value="">请选择</option>
                {foreach $category as $k=>$v}
                <option value="{$k}">{$v|raw}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div id="content" class="layui-input-block">

        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="publish">发布文章</button>
            <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="draft">保存草稿</button>
        </div>
    </div>
</form>
<script src="/static/backend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/backend/js/wangEditor.min.js"></script>
<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 发布文章按钮
        form.on('submit(publish)',function (data) {
            var postData = {
                'title': data.field.title,
                'content': editor.txt.html(),
                'status': 2,
                'category': data.field.category
            };
            $.post('/backend/my-article/save', postData, function (res) {
                if (res.code == 0) {
                    // 添加成功
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000
                    }, function () {
                        // 重新加载父页面
                        parent.location.reload();
                    });
                } else if (res.code == 302) {
                    location.href = '/backend/error/' + res.msg;
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false; // 阻止表单跳转
        });

        // 保存草稿按钮
        form.on('submit(draft)',function (data) {
            var postData = {
                'title': data.field.title,
                'content': editor.txt.html(),
                'status': 1,
                'category': data.field.category
            };
            $.post('/backend/my-article/save', postData, function (res) {
                if (res.code == 0) {
                    // 添加成功
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000
                    }, function () {
                        // 重新加载父页面
                        parent.location.reload();
                    });
                } else if (res.code == 302) {
                    location.href = '/backend/error/' + res.msg;
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false; // 阻止表单跳转
        });

        // 表单验证
        form.verify({
            title: function (value, item) {
                if (value.length > 100) {
                    return '文章标题不得超过100个字符';
                }
            }
        });
    });

    // 富文本编辑器
    const E = window.wangEditor;
    const editor = new E('#content');
    // 配置z-index
    editor.config.zIndex = 500;
    // 配置server接口地址
    editor.config.uploadImgServer = '/backend/my-article/upload-img';
    editor.create();
</script>
</body>
</html>